package com.hoshiicloud.user.controller.organization;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.user.entity.organization.StoreOrganization;
import com.hoshiicloud.user.param.organization.StoreOrganizationAddParam;
import com.hoshiicloud.user.param.organization.StoreOrganizationQueryParam;
import com.hoshiicloud.user.param.organization.StoreOrganizationUpdateParam;
import com.hoshiicloud.user.service.organization.StoreOrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 店铺（伙伴/供应商) 控制器
 * </p>
 *
 * @author campchen
 * @since 2019-04-28
 */
@Slf4j
@RestController
@RequestMapping("/organization/store-organization")
@Api(value = "平台店铺机构", tags = {"平台店铺机构"})
@ApiSort(43)
public class StoreOrganizationController {
    @Autowired
    private StoreOrganizationService storeOrganizationService;

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询店铺（伙伴/供应商)
     */
    @ApiOperation(value = "分页查询店铺伙伴/店铺供应商")
    @PostMapping("/getPage")
    public Response page(@RequestBody StoreOrganizationQueryParam param) {
        try {
            QueryWrapper<StoreOrganization> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq((param.getStoreId() != null && param.getStoreId() != 0), StoreOrganization::getStoreId, param.getStoreId())
                    .eq((param.getOrganizationTypeId() != null), StoreOrganization::getOrganizationTypeId, param.getOrganizationTypeId())
                    .eq((param.getType() != null), StoreOrganization::getType, param.getType())
                    .eq((param.getParentId() != null && param.getParentId() != 0), StoreOrganization::getParentId, param.getParentId())
                    .eq(StringUtils.isNotBlank(param.getName()), StoreOrganization::getName, param.getName())
                    .eq(StringUtils.isNotBlank(param.getCharge()), StoreOrganization::getCharge, param.getCharge())
                    .eq(StringUtils.isNotBlank(param.getIdCard()), StoreOrganization::getIdCard, param.getIdCard())
                    .eq((param.getAccountType() != null), StoreOrganization::getType, param.getType())
                    .eq(StringUtils.isNotBlank(param.getAccountName()), StoreOrganization::getAccountName, param.getAccountName())
                    .eq(StringUtils.isNotBlank(param.getBankNumber()), StoreOrganization::getBankNumber, param.getBankNumber())
                    .eq(StringUtils.isNotBlank(param.getBankName()), StoreOrganization::getBankName, param.getBankName())
                    .eq(StringUtils.isNotBlank(param.getBankAddress()), StoreOrganization::getBankAddress, param.getBankAddress())
                    .eq(StringUtils.isNotBlank(param.getTaxation()), StoreOrganization::getTaxation, param.getTaxation())
                    .eq(StringUtils.isNotBlank(param.getPiaohao()), StoreOrganization::getPiaohao, param.getPiaohao())
                    .eq(StringUtils.isNotBlank(param.getTelphone()), StoreOrganization::getTelphone, param.getTelphone())
                    .eq(StringUtils.isNotBlank(param.getMobile()), StoreOrganization::getMobile, param.getMobile())
                    .eq(StringUtils.isNotBlank(param.getFax()), StoreOrganization::getFax, param.getFax())
                    .eq(StringUtils.isNotBlank(param.getHome()), StoreOrganization::getHome, param.getHome())
                    .eq(StringUtils.isNotBlank(param.getEmail()), StoreOrganization::getEmail, param.getEmail())
                    .eq(StringUtils.isNotBlank(param.getAddress()), StoreOrganization::getAddress, param.getAddress())
                    .eq(StringUtils.isNotBlank(param.getGoodsAddress()), StoreOrganization::getGoodsAddress, param.getGoodsAddress())
                    .eq(StringUtils.isNotBlank(param.getWhouseAddress()), StoreOrganization::getWhouseAddress, param.getWhouseAddress())
                    .eq((param.getPaymentId() != null && param.getPaymentId() != 0), StoreOrganization::getPaymentId, param.getPaymentId())
                    .eq((param.getApplyType() != null), StoreOrganization::getApplyType, param.getApplyType())
                    .eq(StringUtils.isNotBlank(param.getApplyIdNumber()), StoreOrganization::getApplyIdNumber, param.getApplyIdNumber())
                    .eq(StringUtils.isNotBlank(param.getApplyName()), StoreOrganization::getApplyName, param.getApplyName())
                    .eq(StringUtils.isNotBlank(param.getApplyAddress()), StoreOrganization::getApplyAddress, param.getApplyAddress())
                    .eq(StringUtils.isNotBlank(param.getApplyIssuedby()), StoreOrganization::getApplyIssuedby, param.getApplyIssuedby())
                    .eq(StringUtils.isNotBlank(param.getApplySex()), StoreOrganization::getApplySex, param.getApplySex())
                    .eq(StringUtils.isNotBlank(param.getApplyBirthplace()), StoreOrganization::getApplyBirthplace, param.getApplyBirthplace())
                    .eq(StringUtils.isNotBlank(param.getApplyBirthday()), StoreOrganization::getApplyBirthday, param.getApplyBirthday())
                    .eq(StringUtils.isNotBlank(param.getApplyIdFront()), StoreOrganization::getApplyIdFront, param.getApplyIdFront())
                    .eq(StringUtils.isNotBlank(param.getApplyIdBack()), StoreOrganization::getApplyIdBack, param.getApplyIdBack())
                    .eq((param.getChargeTelphone() != null), StoreOrganization::getChargeTelphone, param.getChargeTelphone())
                    .eq((param.getChargeWeixin() != null), StoreOrganization::getChargeWeixin, param.getChargeWeixin())
                    .eq((param.getChargeEmail() != null), StoreOrganization::getChargeEmail, param.getChargeEmail())
                    .eq((param.getProvince() != null), StoreOrganization::getProvince, param.getProvince())
                    .eq(StringUtils.isNotBlank(param.getProvinceName()), StoreOrganization::getProvinceName, param.getProvinceName())
                    .eq((param.getCity() != null), StoreOrganization::getCity, param.getCity())
                    .eq(StringUtils.isNotBlank(param.getCityName()), StoreOrganization::getCityName, param.getCityName())
                    .eq((param.getDistrict() != null), StoreOrganization::getDistrict, param.getDistrict())
                    .eq(StringUtils.isNotBlank(param.getDistrictName()), StoreOrganization::getDistrictName, param.getDistrictName())
                    .eq((param.getStreet() != null), StoreOrganization::getStreet, param.getStreet())
                    .eq((param.getAvailableBalance() != null), StoreOrganization::getAvailableBalance, param.getAvailableBalance())
                    .eq((param.getAccountBalance() != null), StoreOrganization::getAccountBalance, param.getAccountBalance())
                    .eq((param.getFreezingAmount() != null), StoreOrganization::getFreezingAmount, param.getFreezingAmount())
                    .eq((param.getDepartmentId() != null && param.getDepartmentId() != 0), StoreOrganization::getDepartmentId, param.getDepartmentId())
                    .eq(StringUtils.isNotBlank(param.getContacts()), StoreOrganization::getContacts, param.getContacts())
                    .eq(StringUtils.isNotBlank(param.getInvitedCode()), StoreOrganization::getInvitedCode, param.getInvitedCode())
                    .eq(StringUtils.isNotBlank(param.getWeixin()), StoreOrganization::getWeixin, param.getWeixin())
                    .eq(StringUtils.isNotBlank(param.getProfile()), StoreOrganization::getProfile, param.getProfile())
                    .eq((param.getStatus() != null && param.getStatus() != 0), StoreOrganization::getStatus, param.getStatus());
            Page<StoreOrganization> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString() + ",Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(storeOrganizationService.getPage(page, queryWrapper, key));
        } catch (NumberFormatException e) {
            log.error("分页查询店铺（伙伴/供应商)异常", e);
            return Response.failed("分页查询店铺（伙伴/供应商)异常");
        }

    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询店铺（伙伴/供应商)
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询店铺伙伴/店铺供应商")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(storeOrganizationService.getById(id));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增店铺伙伴/店铺供应商")
    public Response saveStoreOrganization(@RequestBody @Valid StoreOrganizationAddParam param) {
        StoreOrganization entity;
        try {
            entity = BeanUtils.covertType(param, StoreOrganization.class);
        } catch (Exception e) {
            log.error("新增店铺（伙伴/供应商)Bean转换异常", e);
            return Response.failed("新增店铺（伙伴/供应商)Bean转换异常");
        }
        return Response.data(storeOrganizationService.insert(entity));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改店铺（伙伴/供应商)
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改店铺伙伴/店铺供应商")
    public Response updateStoreOrganization(@RequestBody @Valid StoreOrganizationUpdateParam param) {
        StoreOrganization entity;
        try {
            entity = BeanUtils.covertType(param, StoreOrganization.class);
        } catch (Exception e) {
            log.error("修改店铺（伙伴/供应商)Bean转换异常", e);
            return Response.failed("修改店铺（伙伴/供应商)Bean转换异常");
        }
        return Response.data(storeOrganizationService.updateById(entity));
    }

    /**
     * @Author:campchen
     * @Description: 供应商审批接口 0-未审批 1-已审批 2-已终止合作
     * @Date:上午 10:26 2019/5/24
     * @Param: id, sataus
     * @return: Response
     */
    @PutMapping("/storeApproval")
    @ApiOperation(value = "店铺供应商审批")
    public Response storeApproval(Long id, Integer status) {
        return Response.data(storeOrganizationService.updateById(id, status));
    }

    /**
     * @param ids
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id组查询店铺（伙伴/供应商) / StringUtils.join(ids, "") 为缓存中添加唯一KEY 如不合适可选择更好方法
     */
    @GetMapping("/ids")
    @ApiOperation(value = "根据id查询店铺（伙伴/供应商)")
    public Response queryById(@RequestParam List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Response.failed("查询店铺（伙伴/供应商)ID集合不能为空");
        }
        ids.sort(null);
        return Response.data(storeOrganizationService.getByIds(ids, StringUtils.join(ids, "")));
    }
}
